package com.fishery.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fishery.base.entity.BaseSupply;
import com.fishery.base.entity.ConstantValue;
import com.fishery.base.entity.SupplyIn;
import com.fishery.base.entity.SupplyOut;
import com.fishery.base.entity.vo.BaseSupplyAccessVo;
import com.fishery.base.mapper.BaseSupplyMapper;
import com.fishery.base.mapper.SupplyInMapper;
import com.fishery.base.service.SupplyInService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fishery.base.util.ExcelUtil;
import com.fishery.entity.GlobalException;
import com.fishery.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Ghost
 * @since 2021-01-15
 */
@Service
public class SupplyInServiceImpl extends ServiceImpl<SupplyInMapper, SupplyIn> implements SupplyInService {

	@Autowired
	private SupplyInMapper supplyInMapper;

	@Autowired
	private BaseSupplyMapper baseSupplyMapper;

	@Override
	public Page<SupplyIn> pageSuppyInRecords(String baseId, int size, int current) {
		if (size <= 0) {
			size = 5;
		}
		if (current <= 0) {
			current = 1;
		}
		Page<SupplyIn> page = new Page<>(current, size);
		QueryWrapper<SupplyIn> wrapper = new QueryWrapper();
		wrapper.eq("base_id", baseId)
				.orderByDesc("gmt_create");
		supplyInMapper.selectPage(page, wrapper);
		return page;
	}

	@Override
	public Page<SupplyIn> pageSuppyInRecords(String baseId, String supplyId, int size, int current) {
		if (size <= 0) {
			size = 5;
		}
		if (current <= 0) {
			current = 1;
		}
		Page<SupplyIn> page = new Page<>(current, size);
		QueryWrapper<SupplyIn> wrapper = new QueryWrapper();
		wrapper.eq("base_id", baseId);
		wrapper.eq("supply_id", supplyId)
				.orderByDesc("gmt_create");
		supplyInMapper.selectPage(page, wrapper);
		return page;
	}

	@Override
	@Transactional
	public boolean updateSupplyInRecord(SupplyIn supplyIn) {
		if (!"渔药".equals(supplyIn.getSupplyTypeName())
				&& !"饲料".equals(supplyIn.getSupplyTypeName())) {
			throw new GlobalException(StatusCode.ERROR, "投入品类型名有误");
		}
		// 原先的投入品入库记录
		SupplyIn oldSupplyIn = supplyInMapper.selectById(supplyIn.getId());
		if (oldSupplyIn == null) {
			return false;
		}
		// 换了一种投入品
		if (StringUtils.isEmpty(supplyIn) || !StringUtils.isEmpty(supplyIn.getSupplyId())
				&& !oldSupplyIn.getSupplyId().equals(supplyIn.getSupplyId())) {
			// 减少原先入库的投入重量
			baseSupplyMapper.decreaseBaseSupply(oldSupplyIn.getBaseId(), oldSupplyIn.getSupplyId(), oldSupplyIn.getInWeight());
			// 删除原先的入库记录
			supplyInMapper.deleteById(supplyIn.getId());
			// 进行新的投入品入库操作
			boolean ret = createSupplyInRecord(supplyIn);
			return ret;
		}
		// 没有换新的一种投入品
		// 判断修改的是否修改了同种投入品的入库的重量，如果是，则需要修改基地的该中投入品的总库存重量
		if (supplyIn.getInWeight() != null
				&& supplyIn.getInWeight().compareTo(ConstantValue.ZERO) > 0
				&& !supplyIn.getInWeight().equals(oldSupplyIn.getInWeight())) {
			// 修改基地投入品总库存信息
			// 获取基地投入品中的该投入品的总库存重量
			QueryWrapper wrapper = new QueryWrapper();
			wrapper.eq("supply_id", oldSupplyIn.getSupplyId());
			wrapper.eq("base_id", oldSupplyIn.getBaseId());
			BaseSupply baseSupply = baseSupplyMapper.selectOne(wrapper);
			// 相差重量
			BigDecimal delt = supplyIn.getInWeight().subtract(oldSupplyIn.getInWeight());
			// 更新数据
			baseSupply.setTotalWeight(baseSupply.getTotalWeight().add(delt));
			baseSupply.setSurplusWeight(baseSupply.getSurplusWeight().add(delt));
			// 更新到数据库中
			baseSupplyMapper.updateById(baseSupply);
		}
		// 更新入库记录到数据库中
		supplyInMapper.updateById(supplyIn);
		return true;
	}

	@Override
	@Transactional
	public boolean createSupplyInRecord(SupplyIn supplyIn) {
		if ("渔药".equals(supplyIn.getSupplyTypeName())
				&& "饲料".equals(supplyIn.getSupplyTypeName())) {
			throw new GlobalException(StatusCode.ERROR, "投入品类型名有误");
		}
		// 保存入库信息
		int ret = supplyInMapper.insert(supplyIn);
		if (ret <= 0) {
			return false;
		}
		// 更新仓库中的库存量
		// 查询该投入品在基地的总库存信息
		QueryWrapper wrapper = new QueryWrapper();
		wrapper.eq("supply_id", supplyIn.getSupplyId());
		wrapper.eq("base_id", supplyIn.getBaseId());
		BaseSupply baseSupply = baseSupplyMapper.selectOne(wrapper);
		// 在基地种添加该投入品信息（1、先前已有该投入品，2、先前未有该种投入品）
		if (baseSupply == null) {
			// 先前未有该种投入品，添加该投入品信息
			baseSupply = new BaseSupply();
			baseSupply.setBaseId(supplyIn.getBaseId());
			baseSupply.setTotalWeight(supplyIn.getInWeight());
			baseSupply.setSurplusWeight(supplyIn.getInWeight());
			baseSupply.setSupplyId(supplyIn.getSupplyId());
			baseSupplyMapper.insert(baseSupply);
		} else {
			// 先前已有该投入品，增加库存重量
			baseSupply.setTotalWeight(baseSupply.getTotalWeight().add(supplyIn.getInWeight()));
			baseSupply.setSurplusWeight(baseSupply.getSurplusWeight().add(supplyIn.getInWeight()));
			baseSupplyMapper.updateById(baseSupply);
		}
		return true;
	}

	@Override
	public void getSupplyInRecordsByExcel(HttpServletResponse response, String baseId) {
		QueryWrapper wrapper = new QueryWrapper();
		wrapper.eq("base_id", baseId);
		ExcelUtil.write(response, "基地投入品入库记录", SupplyOut.class, "投入品入库记录", supplyInMapper.selectList(wrapper));
	}

	@Override
	public List<BaseSupplyAccessVo> getAmountInfoOfBaseSupplyIn(String baseId) {
		return supplyInMapper.findAmountInfoOfBaseSupplyIn(baseId);
	}
}
